package com.jkl.controller.drTable;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.jkl.service.drTable.DrTableGenerateService;
import com.jkl.service.dsRelationship.DsRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.ListId;
import com.jkl.entity.bsAddition.BsAddition;
import com.jkl.entity.bsWorkflowLog.BsWorkflowLog;
import com.jkl.entity.drIndex.DrIndex;
import com.jkl.entity.drIndexFormula.DrIndexFormula;
import com.jkl.entity.drTable.DrTable;
import com.jkl.entity.drTableDetail.DrTableDetail;
import com.jkl.entity.drTemplate.DrTemplate;
import com.jkl.entity.drTemplateDetail.DrTemplateDetail;
import com.jkl.service.drTable.inter.DrTableServiceInterface;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/jky/drTable")
@Slf4j
public class DrTableController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private DrTableServiceInterface DrTableServiceInterface;
	@Autowired
	DrTableGenerateService drTableGenerateService;
	@Autowired
	DsRelationshipService dsRelationshipService;
	
	@PostMapping("/getdrTableList")
	public PageInfo<DrTable> getIndexList(@RequestBody  DrTable drTable) {
		PageInfo<DrTable> drIndexList = DrTableServiceInterface.drTableById(drTable);
		return drIndexList;
	}
	//重复查询  和生成表查询 
	@PostMapping("/isNodrTableList")
	public PageInfo<DrTable> isNodrTableList(@RequestBody  DrTable drTable) {
		PageInfo<DrTable> drIndexList = DrTableServiceInterface.drTableList(drTable);
		return drIndexList;
	}
	

	@Deprecated
	@PostMapping("/saveDrTable")
	public BaseResponseDTO save(@RequestBody DrTable drTable) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		String drTableInt = DrTableServiceInterface.drTablesave(drTable);
		if(drTableInt.equals("1")) {
			returnData.setResultCode("1");
			returnData.setResultMessage("添加成功");
			if(drTable.getDeptName()!=null){
				returnData.setResultMessage("添加成功---"+drTable.getDeptName()+"添加失败");
			}
		}else {
			returnData.setResultCode("0");
			returnData.setResultMessage(drTable.getDeptName()+"添加失败");
		}
		
		return returnData;

	}

	@PostMapping("/generateDrTable")
	public BaseResponseDTO generateTable(@RequestBody DrTable drTable) {
		BaseResponseDTO responseData = new BaseResponseDTO();
		try{
			drTableGenerateService.generateDrTables(drTable);
			responseData.setResultCode("1");
			responseData.setResultMessage("添加成功");
		}catch (Exception ex){
			ex.printStackTrace();
			responseData.setResultCode("0");
			responseData.setResultMessage(ex.getMessage());
		}
		return responseData;
	}

	@PutMapping("/upDrTable")
	public BaseResponseDTO upDate(@RequestBody DrTable drTable) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		int drIndexInt = DrTableServiceInterface.updateDrTable(drTable);
		if(drIndexInt!=0) {
			returnData.setResultMessage("修改成功");
		}else {
			returnData.setResultMessage("修改失败");
		}
		
		return returnData;

	}
	//修改状态
	
	@PutMapping("/updateWorkflowCode")
	public BaseResponseDTO updateWorkflowCode(@RequestBody DrTable drTable) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		DrTable originalTable = dsRelationshipService.targetDrTableById(drTable.getRowId());
		int drIndexInt = DrTableServiceInterface.updateWorkflowCode(drTable);
		int opCode = dsRelationshipService.opCode(originalTable,drTable.getWorkflowCode());
		if(opCode > 0){
			dsRelationshipService.handleDataArchive(originalTable,opCode);
		}
		if(drIndexInt!=0) {
			returnData.setResultMessage("修改成功");
		}else {
			returnData.setResultMessage("修改失败");
		}
		return returnData;
	}
	//修改是否启用
	
	@PutMapping("/updateStatus")
	public BaseResponseDTO updateStatus(@RequestBody DrTable drTable) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		int drIndexInt = DrTableServiceInterface.updateStatus(drTable);
		if(drIndexInt!=0) {
			returnData.setResultMessage("删除成功");
		}else {
			returnData.setResultMessage("删除失败");
		}
		return returnData;
	}
	
	@PostMapping("/deletaDrTable")
	public BaseResponseDTO deletaDrTable(@RequestBody DrTable drTable) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		int drIndexInt = DrTableServiceInterface.deleteDrTable(drTable);
		if(drIndexInt!=0) {
			returnData.setResultMessage("删除成功");
		}else {
			returnData.setResultMessage("删除失败");
		}
		return returnData;

	}
	
	@PostMapping("/deleteTable")
	public int deleteTable(@RequestBody int param) {
		return DrTableServiceInterface.deleteTable(param);
	}
	@PostMapping("/addTables")
	public BaseResponseDTO addTables(@RequestBody ListId listId) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<DrTable> tables=listId.getTables();
		if(tables!=null&&tables.size()!=0) {
			for(DrTable table:tables) {
				DrTable tab=DrTableServiceInterface.queryTable(table.getRowId());
				if(tab==null) {
				DrTableServiceInterface.addTable(table);
				}
				else {
					DrTableServiceInterface.updateTable(table);
				}
			}
		}
		List<DrTableDetail> tableDetails=listId.getTableDetails();
		if(tableDetails!=null&&tableDetails.size()!=0) {
			for(DrTableDetail tabledeta:tableDetails) {
				DrTableDetail tabdet=DrTableServiceInterface.queryTableDetail(tabledeta.getRowId());
				if(tabdet==null) {
				DrTableServiceInterface.addTableDetail(tabledeta);
				}
			}
		}
		List<DrTemplate> templates=listId.getTemplates();
		List<DrTemplateDetail> templateDetails=listId.getTemplateDetails();
		if(templates!=null&&templates.size()!=0) {
			for(DrTemplate template:templates) {
				List<DrTemplateDetail> templateDetails2=new ArrayList();
				if(templateDetails!=null&&templateDetails.size()!=0) {
					for(DrTemplateDetail templadeta:templateDetails) {
					   if(templadeta.getTableId()==template.getRowId()) {
						   templateDetails2.add(templadeta);
						 }
					   }
				   }
				if(DrTableServiceInterface.queryTemplate(template.getRowId())==null) {
				     DrTableServiceInterface.addTemplate(template);
				   
				     DrTableServiceInterface.addTemplateDetails(templateDetails2);
				   //
				}
				else {
					DrTableServiceInterface.setTemplateDetailStatus(template.getRowId()); 
					for(DrTemplateDetail templadeta2:templateDetails2) {
						if(DrTableServiceInterface.queryTemplateDetail(templadeta2.getTableId(),templadeta2.getIndexCode())==null) {
							DrTableServiceInterface.addTemplateDetail(templadeta2);
						}
						else {
							DrTableServiceInterface.setTemplateDetailStatus2(templadeta2.getRowId());
						}
					}
				}
			}
		}
		List<DrIndex> indexs=listId.getIndexs();
		if(indexs!=null&&indexs.size()!=0) {
			for(DrIndex index:indexs) {
				if(DrTableServiceInterface.queryIndex(index.getRowId())==null) {
					DrTableServiceInterface.addIndex(index);
					
				}
				else {
					DrTableServiceInterface.updateIndex(index);
				}
			}
		}
		List<DrIndexFormula> indexFormulas=listId.getIndexFormulas();
		if(indexFormulas!=null&&indexFormulas.size()!=0) {
			for(DrIndexFormula indexformu:indexFormulas) {
				if(DrTableServiceInterface.queryIndexFormula(indexformu.getIndexCode())==null) {
					DrTableServiceInterface.addIndexFormula(indexformu);
				}
				else {
					DrTableServiceInterface.updateIndexFormula(indexformu);
				}
			}
		}
		List<BsWorkflowLog> bsWorkflowLogs=listId.getBsWorkflowLogs();
		if(bsWorkflowLogs!=null&&bsWorkflowLogs.size()!=0) {
		for(BsWorkflowLog bw:bsWorkflowLogs) {
			DrTableServiceInterface.addBsWorkflowLog(bw);
		  }
		}
		return returnData;
	}
	@PostMapping("/queryTables")
	public BaseResponseDTO queryTables(@RequestBody int[] listId) {
		JSONObject returnJson = new JSONObject();
		for(int id:listId) {
			BsWorkflowLog[] workflowLogList=DrTableServiceInterface.queryworkflowLogs(id);
			List<BsAddition> files=new ArrayList();
			for(BsWorkflowLog bw:workflowLogList) {
				if(bw.getIsFile().equals("有")) {
					files.add(DrTableServiceInterface.queryBsAddition(bw.getRowId()));	
				}
			}
			DrTableDetail[] tableDetails=DrTableServiceInterface.queryTableDetails(id);
			returnJson.put("workflowLogList", workflowLogList);
		    returnJson.put("files", files);
			returnJson.put("tableDetails", tableDetails);
		}
		BaseResponseDTO returnData = new BaseResponseDTO();
		returnData.setReturnData(returnJson);
		return returnData;
		
	}
	@PostMapping("/updateTableLog")
	public void updateTableLog(@RequestBody int[] listId) {
		for(int id:listId) {
			DrTableServiceInterface.updateTableLog(id);
		}
	}
	
}
